
//加载方法
$(function (){
    $.ajax({
        url: "/DHMManufacture/queryByCheckTag0",
        type:"get",
        success:function (data){
            $("#dengdai").html(data);
        }
    })
    $.ajax({
        url: "/DHMManufacture/queryByCheckTag1",
        type:"get",
        success:function (data){
            $("#tongguo").html(data);
        }
    })
    $.ajax({
        url: "/DHMManufacture/queryByCheckTag2",
        type:"get",
        success:function (data){
            $("#butongguo").html(data);
        }
    })

    //模糊查询的方法，生成表格
    $("#cxTable").bootstrapTable({
        method:"get",
        contentType:"application/json;charset=utf-8",
        url: "/DHMManufacture/queryAllMManufacture",
        striped: true,
        pageNumber:1,
        pagination:true,
        sidePagination:"server",//设置服务端分页还是客户端分页
        pageSize:5,//页大小
        pageList:[3,5,9],//每页显示的条数
        queryParams:function (params) {
            return {
                pageNum: (params.offset / params.limit) + 1,
                pageSize:params.limit,
                productName:$("#productName").val(),
                startTime:$("#startTime").val(),
                endTime:$("#endTime").val(),
            }
        },
        columns:[
            {title:'生产派工单编号',field:'manufactureId',align:'center'},
            {title:'产品编号',field:'productId',align:'center'},
            {title:'产品名称',field:'productName',align:'center'},
            {title:'数量',field:'amount',align:'center'},
            {title:'登记时间',field:'registerTime',align:'center'},
            {title:'审核时间',field:'checkTime',align:'center'},
            {title:'审核人',field:'checker',align:'center'},
            {title:'审核状态',field:'checkTag',align:'center',
                formatter:function (value,row,index) {
                    if (value=='0'){
                        return "<font color=\"orange\">等待审核</font>";
                    }else if (value=='1'){
                        return "<font color=\"green\">审核通过</font>";
                    }else{
                        return "<font color=\"red\">审核不通过</font>";
                    }
                }
            },
            {title:'操作',field:'id',align:'center',
                formatter:function (value,row,index) {
                    if (row.checkTag=='2') {
                        return "<button class='btn btn-warning' onclick='biangeng("+value+")'>变更</button>";
                    }else{
                        return "<button class='btn btn-primary' onclick='chakan("+value+")'>查看</button>";
                    }
                }
            },
        ]
    })
})


//点击查询
function queryLike(){
    $("#cxTable").bootstrapTable("refresh");
}

//判断是否在前面加0
function getNow(s) {
    return s < 10 ? '0' + s: s;
}

//不通过的重新提交审核
function repeatLook(){
    if (confirm('确定重新提交审核吗？重新提交后将重新进行编号')){
        mManufactureBean.checkTime="";
        mManufactureBean.checker="";
        mManufactureBean.checkTag="0";
        mManufactureBean.remark=$("textarea[name='remark']").val();
        let myDate = new Date();

        let year=myDate.getFullYear();        //获取当前年
        let month=myDate.getMonth()+1;   //获取当前月
        let date=myDate.getDate();            //获取当前日


        let h=myDate.getHours();              //获取当前小时数(0-23)
        let m=myDate.getMinutes();          //获取当前分钟数(0-59)
        let s=myDate.getSeconds();

        let now=year+'-'+getNow(month)+"-"+getNow(date)+" "+getNow(h)+':'+getNow(m)+":"+getNow(s);
        mManufactureBean.registerTime=now;
        $.ajax({
            url:"/DHMManufacture/up",
            type: "put",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(mManufactureBean),
            success:function (data){
                $(".div1").show();
                $(".div2").hide();
                $("#cxTable").bootstrapTable("refresh");
            }
        })
    }
}

//点击查看
function chakan(id){
    //进入单页
    $(".div1").hide();
    $(".div2").show();
    $(".btn1").hide();
    document.getElementById("mManufactureForm").reset();
    //查询产品数量和信息
    $.ajax({
        url:"/DHMManufacture/byId/"+id,
        type:"get",
        dataType:"json",
        success:function (date){
            $("input[name='designer']").val(date.checker);
            $("input[name='register']").val(date.register);
            $("input[name='productId']").val(date.productId);
            $("input[name='applyIdGroup']").val(date.applyIdGroup);
            $("input[name='productName']").val(date.productName);
            $("textarea[name='productDescribe']").val(date.productDescribe);
            $("input[name='amount']").val(date.amount);
            $("textarea[name='remark']").val(date.remark);

            let data = date.mProcedures;
            let tr="";
            for (let i = 0; i < data.length; i++) {
                tr+="<tr>\n" +
                    "                    <td>"+data[i].detailsNumber+"</td>\n" +
                    "                    <td>"+data[i].procedureName+"</td>\n" +
                    "                    <td>"+data[i].procedureId+"</td>\n" +
                    "                    <td>"+data[i].procedureMark+"</td>\n" +
                    "                    <td>"+data[i].labourHourAmount+"</td>\n" +
                    "                    <td>"+data[i].costPrice+"</td>\n" +
                    "                    <td>"+data[i].subtotal+"</td>\n" +
                    "                    <td><a href='#' onclick='watchModule("+data[i].id+")'" +
                    " data-toggle=\"modal\" data-target=\"#myModal\">查看物料</a></td>\n" +
                    "                </tr>";
            }
            $("#tbodyForTr").html(tr);
        }
    })

}


//查看物料
function watchModule(moduleParentId){

    $.ajax({
        url:"/DHMManufacture/queryByParentIdPM",
        type:"get",
        data:{
            parentId:moduleParentId
        },
        dataType:"json",
        success:function (data){
            let tr="";

            for (let i = 0; i < data.length; i++) {
                let unit = data[i].amountUnit;
                if (unit==undefined){
                    unit="";
                }
                tr+="<tr>\n" +
                    "                    <td>"+data[i].detailsNumber+"</td>\n" +
                    "                    <td>"+data[i].productName+"</td>\n" +
                    "                    <td>"+data[i].productId+"</td>\n" +
                    "                    <td></td>\n" +
                    "                    <td>"+data[i].amount+"</td>\n" +
                    "                    <td>"+unit+"</td>\n" +
                    "                    <td>"+data[i].costPrice+"</td>\n" +
                    "                    <td>"+data[i].subtotal+"</td>\n" +
                    "                </tr>";
            }
            $("#moduleWatch").html(tr);
        }
    })
}

//返回列表
function returnList(){
    $(".div1").show();
    $(".div2").hide();
}

//点击变更
let mManufactureBean={};
function biangeng(id){
    $(".btn1").show();
    $(".div1").hide();
    $(".div2").show();
    $("textarea[name='remark']").removeAttr("readonly");
    mManufactureBean.id=id;
    document.getElementById("mManufactureForm").reset();
    //查询产品数量和信息
    $.ajax({
        url:"/DHMManufacture/byId/"+id,
        type:"get",
        dataType:"json",
        success:function (date){
            $("input[name='designer']").val(date.checker);
            $("input[name='register']").val(date.register);
            $("input[name='productId']").val(date.productId);
            $("input[name='applyIdGroup']").val(date.applyIdGroup);
            $("input[name='productName']").val(date.productName);
            $("textarea[name='productDescribe']").val(date.productDescribe);
            $("input[name='amount']").val(date.amount);
            $("textarea[name='remark']").val(date.remark);

            let data = date.mProcedures;
            let tr="";
            for (let i = 0; i < data.length; i++) {
                tr+="<tr>\n" +
                    "                    <td>"+data[i].detailsNumber+"</td>\n" +
                    "                    <td>"+data[i].procedureName+"</td>\n" +
                    "                    <td>"+data[i].procedureId+"</td>\n" +
                    "                    <td>"+data[i].procedureMark+"</td>\n" +
                    "                    <td>"+data[i].labourHourAmount+"</td>\n" +
                    "                    <td>"+data[i].costPrice+"</td>\n" +
                    "                    <td>"+data[i].subtotal+"</td>\n" +
                    "                    <td><a href='#' onclick='watchModule("+data[i].id+")'" +
                    " data-toggle=\"modal\" data-target=\"#myModal\">查看物料</a></td>\n" +
                    "                </tr>";
            }
            $("#tbodyForTr").html(tr);
        }
    })
    $.ajax({
        url:"/DHMManufacture/getNewMaxId",
        type:"get",
        success:function (data){
            mManufactureBean.manufactureId=data;
        }
    })
}